package com.yinhai.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;

/**
 * 此demo用来展示如何结合定位SDK实现定位，并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
 * 
 */
public class OtherPosLocation extends Activity{

	// 定位相关
	//LocationClient mLocClient;
	BitmapDescriptor mCurrentMarker;

	MapView mMapView;
	BaiduMap mBaiduMap;

	// UI相关
	OnCheckedChangeListener radioButtonListener;
	Button requestLocButton;
	boolean isFirstLoc = true;// 是否首次定位
	
	String myaddress = "";
	
	private Intent mIntent;
	private double mLatitude;
	private double mLongitude;
	private String name;
	
	private Button pos_cancel;
	
	Marker marker;
	
	GeoCoder mSearch = null;
	LatLng p;
	View map_tt;
	TextView map_bubbleText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.otherposition);
		
		pos_cancel = (Button)findViewById(R.id.pos_cancel);
		pos_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		mIntent = getIntent();
		mLatitude = Double.parseDouble(mIntent.getStringExtra("latitude"));
		mLongitude = Double.parseDouble(mIntent.getStringExtra("longitude"));
		name = mIntent.getStringExtra("name");
		
		LayoutInflater inflater = LayoutInflater.from(OtherPosLocation.this); 
	    View view = inflater.inflate(R.layout.pos_tips, null);
	    map_tt = view.findViewById(R.id.map_tt);
	    map_bubbleText = (TextView)view.findViewById(R.id.map_bubbleText);
	    map_bubbleText.setText(name);
		

		// 地图初始化
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		/*mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);        // 打开gps  
		option.setCoorType("bd09ll");   // 设置坐标类型，总共有几个类型具体的可以查看百度文档说明  
		option.setScanSpan(1000);      // 设置定位间隔时间  
		option.setAddrType("all");      // 设置返回类型，all = 表示返回所有的数据  
		option.setPriority(LocationClientOption.NetWorkFirst); 
		mLocClient.setLocOption(option);
		mLocClient.start();*/
		LatLng locPoint = new LatLng(mLatitude,mLongitude);
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(map_tt);
		OverlayOptions options = new MarkerOptions()
		.position(locPoint) //设置marker的位置
		.icon(bitmap) //设置marker图标
		.zIndex(18); //设置marker所在层级
		//.draggable(true); //设置手势拖拽
		
		//在地图上添加Marker，并显示
		//mBaiduMap.addOverlay(options);
		marker = (Marker) mBaiduMap.addOverlay(options);
		MapStatus mMapStatus = new MapStatus.Builder()
        .target(marker.getPosition())
        .zoom(18)
        .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
	    MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
	    //改变地图状态
	    mBaiduMap.animateMapStatus(mMapStatusUpdate); 
	}

	
	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// 退出时销毁定位
		//mLocClient.stop();
		// 关闭定位图层
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}

}
